/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import TimeRecording.enums.MonthStatus;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author david
 */
@Entity
@Table(name = "monthly_control")
@NamedQueries({
    @NamedQuery(
            name = "MonthlyControl.findAll",
            query = "SELECT m " +
                    "FROM MonthlyControl m"),
    @NamedQuery(
            name = "MonthlyControl.findByStartDate",
            query = "SELECT m " +
                    "FROM MonthlyControl m " +
                    "WHERE m.startDate = :startDate"),
    @NamedQuery(
            name = "MonthlyControl.findByEndDate",
            query = "SELECT m " +
                    "FROM MonthlyControl m " +
                    "WHERE m.endDate = :endDate"),
    @NamedQuery(
            name = "MonthlyControl.findLatest",
            query = "SELECT m " +
                    "FROM MonthlyControl m " +
                    "ORDER BY m.startDate DESC")})

public class MonthlyControl implements Serializable {

    private static final long serialVersionUID = 1L;

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    } // </editor-fold>

    @Id
    @Column(name = "start_date")
    @Temporal(TemporalType.DATE)
    private Date startDate;

    @Column(name = "end_date")
    @Temporal(TemporalType.DATE)
    private Date endDate;

    @Column(name = "sessional_start_date")
    @Temporal(TemporalType.DATE)
    private Date sessionalStartDate;

    @Column(name = "sessional_end_date")
    @Temporal(TemporalType.DATE)
    private Date sessionalEndDate;

    @Column(name = "status")
    private MonthStatus status;

    public MonthlyControl(Date startDate, Date endDate,
            Date sessionalStartDate, Date sessionalEndDate) {
        this();
        this.startDate = startDate;
        this.endDate = endDate;
        this.sessionalStartDate = sessionalStartDate;
        this.sessionalEndDate = sessionalEndDate;
    }

    public MonthlyControl() {
        status = MonthStatus.IN_PROGRESS;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartdDate(Date startDate) {
        Date oldStartDate = this.startDate;
        this.startDate = startDate;
        changeSupport.firePropertyChange("startDate", oldStartDate, startDate);
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        Date oldEndDate = this.endDate;
        this.endDate = endDate;
        changeSupport.firePropertyChange("endDate", oldEndDate, endDate);
    }

    public Date getSessionalStartDate() {
        return sessionalStartDate;
    }

    public void setSessionalStartDate(Date sessionalStartDate) {
        Date oldSessionalStartDate = this.sessionalStartDate;
        this.sessionalStartDate = sessionalStartDate;
        changeSupport.firePropertyChange("sessionalStartDate", oldSessionalStartDate, sessionalStartDate);
    }

    public Date getSessionalEndDate() {
        return sessionalEndDate;
    }

    public void setSessionalEndDate(Date sessionalEndDate) {
        Date oldSessionalEndDate = this.sessionalEndDate;
        this.sessionalEndDate = sessionalEndDate;
        changeSupport.firePropertyChange("sessionalEndDate", oldSessionalEndDate, sessionalEndDate);
    }

    public MonthStatus getStatus() {
        return status;
    }

    public void setStatus(MonthStatus status) {
        MonthStatus oldStatus = this.status;
        this.status = status;
        changeSupport.firePropertyChange("status", oldStatus, status);
    }

    @Override
    public String toString() {
        return "MonthlyControl{" +
                "startDate=" + startDate + " " +
                "endDate=" + endDate + '}';
    }

    public static MonthlyControl get(EntityManager entityManager,
            Date date, boolean byStartDate) {

        List<MonthlyControl> list = null;
        if (byStartDate) {
            list = entityManager.createNamedQuery("MonthlyControl.findByStartDate")
                    .setParameter("startDate", date)
                    .getResultList();
        } else {
            list = entityManager.createNamedQuery("MonthlyControl.findByEndDate")
                    .setParameter("endDate", date)
                    .getResultList();
        }

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public static MonthlyControl getLatest(EntityManager entityManager) {

        List<MonthlyControl> list = null;
        list = entityManager.createNamedQuery(
                "MonthlyControl.findLatest").getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public static MonthlyControl getLatestButOne(EntityManager entityManager) {

        List<MonthlyControl> list = null;
        list = entityManager.createNamedQuery(
                "MonthlyControl.findLatest").getResultList();

        if (list != null && list.size() > 1) {
            return list.get(1);
        } else {
            return null;
        }
    }

}
